import type { ReqLogin } from './types'; // 导入 ReqLogin

import { requestClient } from '../request';

// ABP应用配置
export function getAbpApplicationConfiguration() {
  return requestClient.get<any>('/api/abp/application-configuration');
}

// ABP会话管理
export function getCurrentLoginInformation() {
  return requestClient.get<any>('/api/abp/application-configuration');
}

// // ABP身份认证 (旧的，将被替换)
// export function login(username: string, password: string) {
//   return requestClient.post<any>('/api/account/login', {
//     userNameOrEmailAddress: username,
//     password,
//     rememberMe: true,
//   });
// }

/**
 * @name AuthModule
 */
// 登录接口 (根据用户提供的新逻辑)
export const login = (params: ReqLogin) => {
  const urlencoded = new URLSearchParams();
  urlencoded.append('client_id', 'Web_App');
  urlencoded.append('client_secret', 'Wms.Web.App');
  urlencoded.append('grant_type', 'password');
  urlencoded.append('username', params.username || '');
  urlencoded.append('password', params.password || '');
  urlencoded.append('scope', 'Manager');
  // BaseUrlAPI 不需要，requestClient 已包含基础路径
  return requestClient.postForm<any>(`/connect/token`, urlencoded);
};

// 登出
export function logout() {

  return requestClient.get<any>('/api/account/logout', {

  });
}

// 获取用户权限
export function getUserPermissions() {
  return requestClient.get<any>('/api/permission-management/permissions', {
    params: {
      providerName: 'R', // 修正为 'U' (User) 或 'R' (Role) 根据需要，通常是 'U'
      providerKey: '',
    },
  });
}

// 获取租户
export function getTenants(params: any) {
  return requestClient.get<any>('/api/multi-tenancy/tenants', {
    params,
  });
}

// 切换租户
export function switchTenant(tenantId: string) {
  return requestClient.put<any>('/api/abp/session/switch-tenant', {
    tenantId,
  });
}

// 获取当前用户信息 (根据用户提供)
export const GetcurrentUser = async () => {
  // BaseUrlAPI 不需要
  return await requestClient.get('/api/abp/application-configuration');
};

// Get button permissions (根据用户提供)
export const GetSetting = async () => {
  // BaseUrlAPI 不需要
  return await requestClient.get('/api/mes/sys/setting');
};

// (根据用户提供)
export async function EditpassWord(options: any) {
  // BaseUrlAPI 不需要
  return requestClient.post('/api/app/sys-user/edit-pass-word', options);
}
